package com.sangeng.utils;

import com.sangeng.domain.entity.LoginUser;
import com.sangeng.domain.entity.User;
import com.sun.org.apache.xpath.internal.operations.Bool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * ClassName: SecurityUtils
 * Package: com.sangeng.utils
 * Description:
 *
 * @Author hjh
 * @Create 2024/8/16 22:53
 * @Version 1.0
 */
@Slf4j
public class SecurityUtils {
    /**
     * 获取用户
     */
    public static LoginUser getLoginUser(){
        LoginUser loginUser=null;
        try {
            loginUser=(LoginUser) getAuthentication().getPrincipal();
        } catch (Exception e) {
            log.warn("定时任务执行");
        }
        if (loginUser==null){
            loginUser=new LoginUser();
            User user=new User();
            user.setId(1L);
            user.setUserName("sg");
            loginUser.setUser(user);
        }

        return loginUser;
    }

    /**
     * 获取Authentication
     */

    public static Authentication getAuthentication(){
        return SecurityContextHolder.getContext().getAuthentication();
    }

    public static Boolean isAdmin(){
        Long id = getLoginUser().getUser().getId();
        return id != null &&1L ==id;
    }

    public static  Long getUserId(){
        return getLoginUser().getUser().getId();
    }

}
